#include "mq_connection.hpp"

void cb(const wsmq::Channel::ptr& channel, const std::string& consumer_tag, const wsmq::BasicProperties* bp, const std::string& body)
{
    std::cout << consumer_tag << "消费了消息：" << body << std::endl;
    channel->basicAck(bp->id());
}
int main(int argc, char* argv[])
{
    if(argc != 2)
    {
        std::cout << "Usage:\n\t./consume_client queue_name\n" << std::endl;
        return -1;
    }
    // 实例化异步工作线程对象
    wsmq::AsyncWorker::ptr worker = std::make_shared<wsmq::AsyncWorker>();
    // 实例化连接对象
    wsmq::Connection::ptr connection = std::make_shared<wsmq::Connection>("127.0.0.1", 8085, worker);
    // 通过连接创建信道
    wsmq::Channel::ptr channel = connection->openChannel();
    google::protobuf::Map<std::string, std::string> tmp_map;
    // 1. 通过信道声明交换机exchange1，交换类型为广播交换
    channel->declareExchange("exchange1", wsmq::ExchangeType::TOPIC, true, false, tmp_map);
    // 2. 通过信道声明队列queue1
    channel->declareQueue("queue1", true, false, false, tmp_map);
    // 3. 通过信道声明队列queue2
    channel->declareQueue("queue2", true, false, false, tmp_map);
    // 4. 通过信道绑定exchange1-queue1，binding_key为queue1；
    channel->queueBind("exchange1", "queue1", "queue1");
    // 5. 通过信道绑定exchange1-queue2，binding_key为news.music.#
    channel->queueBind("exchange1", "queue2", "news.music.#");
    
    // 通过信道订阅消息
    // auto functor = std::bind(&cb, channel, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
    auto functor = std::bind(cb, channel, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
    channel->basicConsume("consumer1", argv[1], false, functor);
    while(1) std::this_thread::sleep_for(std::chrono::seconds(3));
    // 关闭信道
    connection->closeChannel(channel);
    return 0;
}